package com.sunfeng.n4.reentrant;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 锁超时
 *
 * @author sunfeng
 * @date 2021/03/30 13:47
 **/

@Slf4j(topic = "c.ReentrantLockTest3")
public class ReentrantLockTest3 {

    private static ReentrantLock reentrantLock = new ReentrantLock();

    public static void main(String[] args) throws InterruptedException {

        Thread thread = new Thread(() -> {
            try {
                boolean b = reentrantLock.tryLock(2, TimeUnit.SECONDS);
                if(b){
                    log.debug("规定时间内获取到锁");
                }else{
                    log.debug("规定时间内没有获取到锁");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t1");

        try {
            reentrantLock.lock();
            thread.start();
            TimeUnit.SECONDS.sleep(1);
        }finally {
            reentrantLock.unlock();
        }



    }
}
